#include "print.h"
#include "init.h"
#include "debug.h"
#include "memory.h"
#include "thread.h"
#include "interrupt.h"
#include "console.h"
#include "process.h"

#include "ioqueue.h"
#include "keyboard.h"

void k_thread_a(void*);
void k_thread_b(void*);
void u_prog_a(void);
void u_prog_b(void);
int test_var_a = 0, test_var_b = 0; // at kernel space

int main(void){
	put_str("I am kernel\n");
	init_all();

	/*interrupt.c*/
	//asm volatile ("sti");      //open interrupt
	//ASSERT(1==2);
	
	/*memory.c*/
	//void* vaddr = get_kernel_pages(3);
	//put_str("get_kernel_page start vaddr is:");
	//put_int((int)vaddr);
	//put_char('\n');

	thread_start("k_kernel_a",31,k_thread_a,"A_");
	thread_start("k_kernel_b",31,k_thread_b,"B_");
	process_execute(u_prog_a, "user_prog_a");
	process_execute(u_prog_b, "user_prog_b");

	intr_enable();

	while(1);// {
	//	console_put_str("Main ");
//	}
	return 0;
}

void k_thread_a(void* arg) {
	char* para = arg;
	while(1) {
		console_put_str(" v_a:0x");
		console_put_int(test_var_a);
	}
}

void k_thread_b(void* arg) {
	char* para = arg;
	while(1) {
		console_put_str(" v_b:0x");
		console_put_int(test_var_b);
	}
}

void u_prog_a(void) {
	while(1) {
		test_var_a++;
	}
}

void u_prog_b(void) {
	while(1) {
		test_var_b++;
	}
}

/*void k_thread_a(void* arg) {
	char* para = (char*)arg;
	while(1) {
		enum intr_status old_status = intr_disable();
		if(!ioq_empty(&kbd_buf)) {
			console_put_str(para);
			char byte = ioq_getchar(&kbd_buf);
			console_put_char(byte);
			console_put_char(' ');
		}
		intr_set_status(old_status);
	}
}

void k_thread_b(void* arg) {
	char* para = (char*)arg;
	while(1) {
		enum intr_status old_status = intr_disable();
		if(!ioq_empty(&kbd_buf)) {
			console_put_str(para);
			char byte = ioq_getchar(&kbd_buf);
			console_put_char(byte);
			console_put_char(' ');
		}
		intr_set_status(old_status);
	}
}*/
